/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the lkyh.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.lkyh.mes.codegen.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkyh.mes.codegen.entity.GenDatasourceConf;
import com.lkyh.mes.codegen.entity.GenTableInfo;
import com.lkyh.mes.codegen.service.GenDatasourceConfService;
import com.lkyh.mes.codegen.service.GenTableInfoService;
import com.lkyh.mes.common.core.util.R;
import com.lkyh.mes.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 表单管理
 *
 * @author lengleng
 * @date 2019-08-12 15:55:35
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tableinfo")
@Api(value = "tableinfo", tags = "表管理")
public class GenTableInfoController {
	private final GenTableInfoService genTableInfoService;
	private GenDatasourceConfService genDatasourceConfService;

	/**
	 * 分页查询
	 *
	 * @param page         分页对象
	 * @param genTableInfo 生成记录
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R getGenTableInfoPage(Page page, GenTableInfo genTableInfo) {
		return R.ok(genTableInfoService.page(page, Wrappers.query(genTableInfo)));
	}

	/**
	 * 分页查询
	 *
	 * @param genTableInfo 生成记录
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/list")
	public R getGenTableInfoList(GenTableInfo genTableInfo) {
		return R.ok(genTableInfoService.list(Wrappers.query(genTableInfo)));
	}

	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/tree")
	public R getGenTableInfoTree() {
		List<GenDatasourceConf> genDatasourceConfList = genDatasourceConfService.list();
		List<GenTableInfo> genTableInfoList = genTableInfoService.list();
		genDatasourceConfList.forEach(i -> {
			i.setGenTableInfoList(genTableInfoList.stream().filter(t -> i.getId().equals(t.getDatasourceId())).collect(Collectors.toList()));
		});
		return R.ok(genDatasourceConfList);
	}

	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/treeWithChild")
	public R getGenTableInfoTreeWithChild(GenTableInfo genTableInfo) {
		List<GenDatasourceConf> genDatasourceConfList = genDatasourceConfService.list(Wrappers.query(new GenDatasourceConf().setId(genTableInfo.getDatasourceId())));
		List<GenTableInfo> genTableInfoList = genTableInfoService.listWithChild(Wrappers.query(genTableInfo));
		genDatasourceConfList.forEach(i -> {
			i.setGenTableInfoList(genTableInfoList.stream().filter(t -> i.getId().equals(t.getDatasourceId())).collect(Collectors.toList()));
		});
		return R.ok(genDatasourceConfList);
	}

	/**
	 * 通过id查询生成记录
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Integer id) {
		return R.ok(genTableInfoService.getById(id));
	}

	/**
	 * 通过id查询生成记录
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/getByIdWithChild/{id}")
	public R getByIdWithChild(@PathVariable("id") Integer id) {
		return R.ok(genTableInfoService.getByIdWithChild(id));
	}

	/**
	 * 新增生成记录
	 *
	 * @param genTableInfo 生成记录
	 * @return R
	 */
	@ApiOperation(value = "新增表记录", notes = "新增表记录")
	@SysLog("新增表记录")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('gen_table_info_add')")
	public R save(@RequestBody GenTableInfo genTableInfo) {
		return R.ok(genTableInfoService.save(genTableInfo));
	}

	/**
	 * 修改生成记录
	 *
	 * @param genTableInfo 生成记录
	 * @return R
	 */
	@ApiOperation(value = "修改表记录", notes = "修改表记录")
	@SysLog("修改表记录")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('gen_table_info_edit')")
	public R updateById(@RequestBody GenTableInfo genTableInfo) {
		return R.ok(genTableInfoService.updateById(genTableInfo));
	}

	/**
	 * 通过id删除生成记录
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除表记录", notes = "通过id删除表记录")
	@SysLog("通过id删除表记录")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('gen_table_info_del')")
	public R removeById(@PathVariable Integer id) {
		genTableInfoService.dropTable(id);
		return R.ok();
	}

	/**
	 * 同步
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "同步表记录", notes = "同步表记录")
	@SysLog("同步表记录")
	@PostMapping("/sync/{id}")
	@PreAuthorize("@pms.hasPermission('gen_table_info_sync')")
	public R sync(@PathVariable Integer id) {
		return R.ok(genTableInfoService.sync(id));
	}

}
